/*
 * cube_functions.c
 *
 *  Created on: Jan 28, 2012
 *      Author: a0272955
 */

#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h" 
#include "utils/uartstdio.h"
#include "driverlib/gpio.h"
#include "inc/hw_ssi.h"
#include "driverlib/ssi.h"
#include "cube_functions.h" 


unsigned long currentLayerGPIO[NUM_LAYERS];

unsigned long g_ulImageTime = 5;

unsigned long g_ulLayerIndex = 0;

int g_updateFlag = 0;


unsigned long g_ulDataRx[];

unsigned long cube[NUM_LAYERS][NUM_LAYERS];
unsigned long cubeWrite[NUM_LAYERS][NUM_LAYERS];



//*****************************************************************************
//
// Turn Single LED ON
//
//*****************************************************************************
void
SingleLED(int column, int row, int layer)
{

	//TODO
/*
	g_ulLayerArrays[layer][i]


    for(i=0;i<NUM_ICS;i++)
    {
        Send();
	}
	
	Latch();
*/
}

//*****************************************************************************
//
// Latch the SPI data to the LED ICs.
//
//*****************************************************************************
void
Latch(void)
{
	// Toggle GPIO pin to latch data.
	ROM_GPIOPinWrite(LATCH_PORT , LATCH_PIN, LATCH_PIN);
	SysCtlDelay(100);
	ROM_GPIOPinWrite(LATCH_PORT , LATCH_PIN, 0);
}

//*****************************************************************************
//
// Send the SPI data to the LED ICs.
//
//*****************************************************************************
void
Send(unsigned long Data)
{
	ROM_SSIDataPut(SSI3_BASE, Data);
	
	while(ROM_SSIBusy(SSI3_BASE))
	{
	}
}

//*****************************************************************************
//
// Turn layer ON/OFF
//
//*****************************************************************************
void
SetLayerState(unsigned long ulLayer,  unsigned char cstate)
{
	if (cstate == 1)
	{
	    ROM_GPIOPinWrite(LAYER_PORT , currentLayerGPIO[ulLayer],
	                     currentLayerGPIO[ulLayer]);
	    
	}
	else
	{
		ROM_GPIOPinWrite(LAYER_PORT , currentLayerGPIO[ulLayer],
                         0);
	}
}

//*****************************************************************************
//
// Write current layer (g_ulLayerIndex) data to ICs
//
//*****************************************************************************
void
WriteLayer(void)
{
    unsigned short i=0;


    for(i=8; i>0; i-=2)
	{
    	Send(cubeWrite[g_ulLayerIndex][i-1]<<8 | cubeWrite[g_ulLayerIndex][i-2]);

    }

    
    SysCtlDelay(2500);
    
	Latch();
}


void
switch_buff(void){

	int l,m;
	for(l = 0;l<NUM_LAYERS;l++){
		for(m = 0;m<NUM_LAYERS;m++){
			cubeWrite[l][m] = cube[l][m];
		}
	}
}






void
InitLayers(void){



    currentLayerGPIO[0] = LAYER_7_PIN;
    currentLayerGPIO[1] = LAYER_0_PIN;
    currentLayerGPIO[2] = LAYER_1_PIN;
    currentLayerGPIO[3] = LAYER_2_PIN;
    currentLayerGPIO[4] = LAYER_3_PIN;
    currentLayerGPIO[5] = LAYER_4_PIN;
    currentLayerGPIO[6] = LAYER_5_PIN;
    currentLayerGPIO[7] = LAYER_6_PIN;


}

